package org.telegram.bot.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.telegram.bot.base.BotRegisterApplication;
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication;
import org.telegram.telegrambots.longpolling.util.TelegramOkHttpClientFactory;

import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;

/**
 * @Author: wyx
 * @Date: 2024/11/29 23:47
 */
@Configuration
public class BotConfiguration {
    
    @Value("${proxy.enabled:false}")
    private Boolean enabled;
    
    //@Bean(value = "okClient") // 如果不想使用 HTTP 代理，请注释此行
    public OkHttpClient okClientHttp(
            @Value("${proxy.hostname}") String hostname,
            @Value("${proxy.port}") int port,
            @Value("${proxy.username:squid}") String username,
            @Value("${proxy.password}") String password
    ) {
        if (enabled) {
            return new TelegramOkHttpClientFactory.HttpProxyOkHttpClientCreator(
                    () -> new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port)),
                    () -> (route, response) -> {
                        String credential = Credentials.basic(username, password);
                        return response
                                .request()
                                .newBuilder()
                                .header("Proxy-Authorization", credential)
                                .build();
                    }
            ).get();
        }else{
            return new TelegramOkHttpClientFactory.DefaultOkHttpClientCreator().get();
        }
    }
    
    @Bean(value = "okClient") // 如果使用SOCKS5，请取消注释此行
    public OkHttpClient okClientSocks(
            @Value("${proxy.hostname}") String hostname,
            @Value("${proxy.port}") int port,
            @Value("${proxy.username}") String username,
            @Value("${proxy.password}") String password
    ) {
        //启用代理
        if(enabled) {
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    if (getRequestingHost().equalsIgnoreCase(hostname)) {
                        if (port == getRequestingPort()) {
                            return new PasswordAuthentication(username, password.toCharArray());
                        }
                    }
                    return null;
                }
            });
            
            return new TelegramOkHttpClientFactory.SocksProxyOkHttpClientCreator(
                    () -> new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(hostname, port))
            ).get();
        }else{
            return new TelegramOkHttpClientFactory.DefaultOkHttpClientCreator().get();
        }
    }
    
    @Bean(value = "telegramBotsApplication")
    public TelegramBotsLongPollingApplication telegramBotsApplication(
            @Qualifier("okClient") OkHttpClient okClient
    ) {
        return new TelegramBotsLongPollingApplication(ObjectMapper::new, () -> okClient);
    }
    
    /**
     * 注册机器人实例
     * @return
     */
    @Bean(value = "botRegisterApplication")
    public BotRegisterApplication botRegisterApplication(){
        return new BotRegisterApplication();
    }
}
